#include "stdafx.h"
#include "IntroView.h"

#include "GlobalAppData.h"
#include "InputLayouts.h"
#include "LoadShader.h"

#include "Renderer.h"

using namespace intro;

mgfx::ShaderID shader;

void IntroView::init()
{
	View::load();

	// vertex buffer
	mgfx::VertexPositionTexture vertices[4];

	vertices[0].Position = XMFLOAT3(-1.0f, -1.0f, 0.5f);
	vertices[1].Position = XMFLOAT3(-1.0f,  1.0f, 0.5f);
	vertices[2].Position = XMFLOAT3( 1.0f, -1.0f, 0.5f);
	vertices[3].Position = XMFLOAT3( 1.0f,  1.0f, 0.5f);

	vertices[0].TextureCoord = XMFLOAT2(-1.0f, -1.0f);
	vertices[1].TextureCoord = XMFLOAT2(-1.0f,  1.0f);
	vertices[2].TextureCoord = XMFLOAT2( 1.0f, -1.0f);
	vertices[3].TextureCoord = XMFLOAT2( 1.0f,  1.0f);

	D3D11_BUFFER_DESC desc;
	desc.Usage = D3D11_USAGE_IMMUTABLE;
	desc.ByteWidth = sizeof(mgfx::VertexPositionTexture) * 4;
	desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
	desc.CPUAccessFlags = 0;
	desc.MiscFlags = 0;

	D3D11_SUBRESOURCE_DATA data;
	data.pSysMem = vertices;
	data.SysMemPitch = 0;
	data.SysMemSlicePitch = 0;

	//HR(device->CreateBuffer(&desc, &data, &mVertexBuffer));
	
	// index buffer
	short indices[6];

	indices[0] = 0;
	indices[1] = 1;
	indices[2] = 2;

	indices[3] = 1;
	indices[4] = 3;
	indices[5] = 2;

	desc.Usage = D3D11_USAGE_IMMUTABLE;
	desc.ByteWidth = sizeof(short) * 6;
	desc.BindFlags = D3D11_BIND_INDEX_BUFFER;

	data.pSysMem = indices;

	//HR(device->CreateBuffer(&desc, &data, &mIndexBuffer));

	// effect
	//mHLSLEffect = mgfx::HLSLEffect::createFromFile("intro.fx");

	shader = mgfx::loadShaderFromFile("intro.fx");
}

void IntroView::unload()
{
	//ReleaseCOM(mVertexBuffer);
	//ReleaseCOM(mIndexBuffer);

	View::unload();
}

void IntroView::draw()
{
	uint stride = sizeof(mgfx::VertexPositionTexture);
	uint offset = 0;

	//context->IASetInputLayout(mgfx::VertexPositionTexture::InputLayout);
	//context->IASetVertexBuffers(0, 1, &mVertexBuffer, &stride, &offset);
	//context->IASetIndexBuffer(mIndexBuffer, DXGI_FORMAT_R16_UINT, 0);
	//context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);


	//mHLSLEffect->Effect->GetVariableByName("gTime")->AsScalar()->SetFloat(mTotalTime);


	//ID3DX11EffectTechnique * effectTech = mHLSLEffect->Effect->GetTechniqueByIndex(0);

	//D3DX11_TECHNIQUE_DESC techDesc;
	//HR(effectTech->GetDesc( &techDesc ));

	//for(uint j = 0; j < techDesc.Passes; ++j)
	//{
	//	ID3DX11EffectPass* pass = effectTech->GetPassByIndex(j);
	//	pass->Apply(0, context);
	//	context->DrawIndexed(6, 0, 0);
	//}


	mgfx::Renderer * rdr = mgfx::Renderer::Instance;
	rdr->setShader(shader);
	rdr->setShaderFloat("gTime", mTotalTime);
	rdr->drawFullScreenQuad();
	
}